package servlet;

import dao.StudentD;
import dao.TeacherD;
import vo.Student;
import vo.Teacher;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

// 处理登录信息, 若成功则跳转对应身份的操作界面, 否则给出错误提示
@WebServlet("/check_login")
public class check_login extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码方式
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();

        String user = request.getParameter("user");
        String password = request.getParameter("password");
        String role = request.getParameter("role");
        String remember = request.getParameter("remember");

        TeacherD teacherD = new TeacherD();
        StudentD studentD = new StudentD();
        Teacher teacher = null;
        Student student = null;
        boolean flag = false;

        try {
            flag = role.equals("teacher");
//            System.out.println(flag);
            // 判断用户身份
            if(flag){
                teacher = teacherD.check(user);
                if(teacher == null){
                    out.print("<script>alert('用户名不存在！');window.location.href='login.jsp';</script>");
                }else {
                    try {
                        teacher = teacherD.checkAccount(user, password);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    // 判断用户密码是否正确
                    if (teacher != null) {
                        //向session中添加用户信息,同时为防跳墙准备
                        session.setAttribute("info", teacher);
                        //检查用户是否需要保持登录状态
                        if (remember != null) {
                            //发送cookie到客户端
                            Cookie un = new Cookie("name", user);
                            Cookie pwd = new Cookie("password", password);
                            un.setMaxAge(60*60*24); //一天
                            pwd.setMaxAge(60*60*24);
                            response.addCookie(un);
                            response.addCookie(pwd);
                        }
                        response.sendRedirect("one_page_student"); // 重定向跳转
                    }else {
                        out.print("<script>alert('密码错误！');window.location.href='login.jsp';</script>");
                    }
                }
            }else {
                student = studentD.check(user);
                if(student == null){
                    out.print("<script>alert('用户名不存在！');window.location.href='login.jsp';</script>");
                }else {
                    try {
                        student = studentD.checkAccount(user, password);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    // 判断用户密码是否正确
                    if (student != null){
                        //向session中添加用户信息设置,同时为防跳墙准备
                        session.setAttribute("info", student);
                        //检查用户是否需要保持登录状态
                        if (remember != null) {
                            //发送cookie到客户端
                            Cookie un = new Cookie("name", user);
                            Cookie pwd = new Cookie("password", password);
                            un.setMaxAge(60*60*24); //一天
                            pwd.setMaxAge(60*60*24);
                            response.addCookie(un);
                            response.addCookie(pwd);
                        }
                        response.sendRedirect("student/main.jsp"); // 重定向跳转到学生主页
                    }else {
                        out.print("<script>alert('密码错误！');window.location.href='login.jsp';</script>");
                    }
                }
            }
        }
        catch (Exception e) {
            out.print(e);
        }
    }
}
